package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.PackageModel;
	import com.micmac.customer.view.PackageView;
	import com.micmac.customer.vo.PackgeTitleVO;
	
	import flash.events.Event;
	
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;
	
	public class PackageViewMediator extends ModuleMediator
	{
		[Inject]
		public var packageView:PackageView;
		[Inject]
		public var service:ICustomerService;
		[Inject]
		public var _dataModel:PackageModel;
		
		private var _packageTitleIndex:int = 0;
		
		override public function onRegister():void
		{
			addViewListener(PackageView.PACKAGE_TITLE,onPackageTitleSave);
			addViewListener(PackageView.PACKAGE_TITLE_PREVIOUS,onPackageTitlePrevious);
			addViewListener(PackageView.PACKAGE_TITLE_NEXT,onPackageTitleNext);
			
			addViewListener(PackageView.SHOW_LIST_VIEW,onShowListView);
			addViewListener(PackageView.SHOW_TILE_VIEW,onShowTileView);
			
			addViewListener(PackageView.ON_BACK,onBack);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.PACKAGE_TITLE_SAVED,onPackageTitleSaved)
		}
		
		private function onPackageTitleSave(event:Event):void
		{
			var packageTitleVO:PackgeTitleVO = new PackgeTitleVO();
			packageTitleVO.name = packageView.packageTitleView.titleNameText.text;
			packageTitleVO.description = packageView.packageTitleView.titleDescriptionText.text;
			packageTitleVO.packageType = packageView.packageTitleView.titlePackageTypeText.text;
			packageTitleVO.pricingModel = packageView.packageTitleView.titlePricingModelText.text;
			packageTitleVO.pricingType = packageView.packageTitleView.pricingTypeText.text;
			packageTitleVO.lastPrice = Number(packageView.packageTitleView.lastPriceText.text);
			packageTitleVO.status = packageView.packageTitleView.statusText.text;
			service.savePackageTitle(packageTitleVO);
		}
		
		private function onPackageTitleSaved(event:CustomerEvent):void
		{
			if(_dataModel.packageTitleList.length != 1 && _dataModel.packageTitleList.length != 0)
			{
				packageView.prevBtn.enabled = true;
			}
			if(_dataModel.packageTitleList.length != 0)
			{
				packageView.listBtn.enabled = true;
				packageView.tileBtn.enabled = true;
			}else
			{
				packageView.listBtn.enabled = false;
				packageView.tileBtn.enabled = false;
			}
			packageView.addPackage.enabled = true;
			packageView.saveBtn.enabled = false;
			_packageTitleIndex = _dataModel.packageTitleList.length - 1;
			packageView.addPackage.label = '+title';
			
		}
		
		private function onPackageTitlePrevious(event:Event):void
		{
			_packageTitleIndex--;
			packageView.nextBtn.enabled = true;
			packageView.saveBtn.enabled = false;
			
			packageView.packageTitleView.titleNameText.text = _dataModel.packageTitleList[_packageTitleIndex].name;
			packageView.packageTitleView.titleDescriptionText.text = _dataModel.packageTitleList[_packageTitleIndex].description;
			packageView.packageTitleView.titlePackageTypeText.text = _dataModel.packageTitleList[_packageTitleIndex].packageType;
			packageView.packageTitleView.titlePricingModelText.text = _dataModel.packageTitleList[_packageTitleIndex].pricingModel;
			packageView.packageTitleView.pricingTypeText.text = _dataModel.packageTitleList[_packageTitleIndex].pricingType;
			packageView.packageTitleView.lastPriceText.text = _dataModel.packageTitleList[_packageTitleIndex].lastPrice;
			packageView.packageTitleView.statusText.text = _dataModel.packageTitleList[_packageTitleIndex].status;
			
			if(_packageTitleIndex == 0)
			{
				packageView.prevBtn.enabled = false;
			}
		}
		
		private function onPackageTitleNext(event:Event):void
		{
			_packageTitleIndex++;
			packageView.saveBtn.enabled = false;
			packageView.prevBtn.enabled = true;
			
			packageView.packageTitleView.titleNameText.text = _dataModel.packageTitleList[_packageTitleIndex].name;
			packageView.packageTitleView.titleDescriptionText.text = _dataModel.packageTitleList[_packageTitleIndex].description;
			packageView.packageTitleView.titlePackageTypeText.text = _dataModel.packageTitleList[_packageTitleIndex].packageType;
			packageView.packageTitleView.titlePricingModelText.text = _dataModel.packageTitleList[_packageTitleIndex].pricingModel;
			packageView.packageTitleView.pricingTypeText.text = _dataModel.packageTitleList[_packageTitleIndex].pricingType;
			packageView.packageTitleView.lastPriceText.text = _dataModel.packageTitleList[_packageTitleIndex].lastPrice;
			packageView.packageTitleView.statusText.text = _dataModel.packageTitleList[_packageTitleIndex].status;
			
			if(_packageTitleIndex == _dataModel.packageTitleList.length - 1)
			{
				packageView.nextBtn.enabled = false;
			}
		}
		
		private function onShowListView(event:Event):void
		{
			switch(packageView.vsPackage.selectedIndex)
			{
				case 0:
					packageView.packageTitleView.packageTitleVs.selectedIndex = 2;
					packageView.packageTitleView.packageTitleList = _dataModel.packageTitleList;
					break;
				case 1:
					
					break;
			}
			packageView.currentState = 'list';
		}
		
		private function onShowTileView(event:Event):void
		{
			switch(packageView.vsPackage.selectedIndex)
			{
				case 0:
					packageView.packageTitleView.packageTitleVs.selectedIndex = 1;
					packageView.packageTitleView.packageTitleList = _dataModel.packageTitleList;
					break;
				case 1:
					
					break;
			}
			packageView.currentState = 'tile';
		}
		
		private function onBack(event:Event):void
		{
			packageView.packageTitleView.packageTitleVs.selectedIndex = 0;
			packageView.currentState = 'normal';
		}
	}
}